﻿using UnityEngine;
using System.Collections;

public class nukeExplode : MonoBehaviour
{
	bool fadeBackIn;
	bool alreadyFading = false;
	public float Range = 10;
	public GameObject nukeprefab;
	public float countdown = 5;
	bool timing;
	// Use this for initialization
	void Start ()
	{
	
		StartCoroutine (FadeTo (0.0f, 1.0f, false));
		timing = true;
		fadeBackIn = false;
	}
	
	void Update ()
	{
		
		
		
	
		if (fadeBackIn == false && (!alreadyFading)) {
			alreadyFading = true;
			//StartCoroutine (FadeTo (0.0f, 1.0f, true));
			//Debug.Log("yo");
		}
		if (fadeBackIn == true && (!alreadyFading)) {
			alreadyFading = true;
			//Debug.Log("yo2");
			StartCoroutine (FadeTo (1.0f, 1.0f, false));
		}
	}
	
	IEnumerator FadeTo (float aValue, float aTime, bool fadeIn)
	{
		//Debug.Log("khlk");
		float alpha = transform.renderer.material.color.a;
		for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / aTime) {
			Color newColor = new Color (1, 1, 1, Mathf.Lerp (alpha, aValue, t));
			transform.renderer.material.color = newColor;
			yield return null;
			
		}
		fadeBackIn = fadeIn;
		alreadyFading = false;
		Destroy(gameObject);
	}
}
